package com.kolon.business.mKolonInt.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonInt.controller.MKolonIntWriteController")
public class MKolonIntWriteController implements SimpleController {

	@RequestMapping(value="/business/mKolonInt/mKolonIntWrite.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
		String returnUrl = "/business/mKolonInt/mKolonIntList.do";
		ModelAndView modelAndView = new ModelAndView();
		if(!"".equals(request.getParameter("title")) && request.getParameter("title")!=null && !"".equals(request.getParameter("htmlbody")) && request.getParameter("htmlbody")!=null) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/kolon_int.aspx?strMode=").append("write");
			buffer.append("&title=").append(ComUtil.getEncode(request.getParameter("title"),"EUC-KR"));
			buffer.append("&htmlbody=").append(ComUtil.getEncode(request.getParameter("htmlbody"),"EUC-KR"));
			buffer.append("&CompanyCD=").append(CompanyCode);
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer.toString(), id, pw);
			modelAndView.addObject("message", "등록되었습니다.");
			modelAndView.addObject("scriptType", "003");
			modelAndView.addObject("message_url", returnUrl);
			modelAndView.setViewName("/business/common/alert");
		}else{
			modelAndView.addObject("strMode", request.getParameter("strMode"));
			modelAndView.addObject("url", url);
		}
		
		return modelAndView;
	}

}